﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mIE.Common
{
    public class GetSerial_No
    {
        public static DataTable Serial_No(DataTable dtSerial, DataTable dtRouting, DataRow drser, DataRow drRouting,
            bool first, int Serial, int Routing_Group, int Calculate, string CountDecription, int Flag, out int GROUP_CODE, out int iCount)
        {
            //GROUP_CODE = 0; iCount = 0;
            //DataRow drTemp = dtRouting.Select("1=1", "Serial ASC").LastOrDefault();//临时表最后一行数据
            drRouting = dtRouting.NewRow();//路线代码组输出表   
            //DataRow[] drTemp2 = dtSerial.Select("Calculate_Desc='" + CountDecription + "'");//主表-dr["CountDecription"]如何取值
            DataRow[] drTemp1 = dtRouting.Select("Calculate_Desc='" + CountDecription + "'");//临时表-dr["CountDecription"]如何取值
            DataRow drow = dtSerial.NewRow();
            if (dtSerial.Rows.Count==0)
            {
                if (Flag == 1)//Laminate--D
                {
                    drRouting["Routing_Group"] = 44000001;
                    drRouting["Calculate"] = 2;
                }
                else if (Flag == 2)//Panel--P
                {
                    drRouting["Routing_Group"] = 43000001;
                    drRouting["Calculate"] = 1;
                }
                else if (Flag == 3)//Seating--S
                {
                    drRouting["Routing_Group"] = 41000001;
                    drRouting["Calculate"] = 1;
                }
                else if (Flag == 4)//MCG--M
                {
                    drRouting["Routing_Group"] = 47000001;
                    drRouting["Calculate"] = 1;
                }
                else if (Flag == 5)//T--
                {
                    drRouting["Routing_Group"] = 46000001;
                    drRouting["Calculate"] = 1;
                }
                else if (Flag == 6)//G--G
                {
                    drRouting["Routing_Group"] = 48000001;
                    drRouting["Calculate"] = 1;
                }
                GROUP_CODE = Convert.ToInt32(drRouting["Routing_Group"]);
                iCount = Convert.ToInt32(drRouting["Calculate"]);
                Routing_Group = Convert.ToInt32(drRouting["Routing_Group"]);
                Calculate = Convert.ToInt32(drRouting["Calculate"]);

                drow["Routing_Group"] = Routing_Group;
                drow["Calculate"] = Calculate;
                drow["Calculate_Desc"] = CountDecription; //如何取值
                dtSerial.Rows.Add(drow);
            }
            else
            {
                DataRow[] drTemp2 = dtSerial.Select(" Calculate_Desc='" + CountDecription + "'");
                if (drTemp2.Length>0)
                {
                    GROUP_CODE = Convert.ToInt32(drTemp2[0]["Routing_Group"]);
                    iCount = Convert.ToInt32(drTemp2[0]["Calculate"]);
                }
                else
                {
                    

                   

                    if (Flag == 1)
                    {

                        DataRow drTemp = dtSerial.Select("1=1 ", "Routing_Group,Calculate ASC").LastOrDefault();
                        Serial = Convert.ToInt32(drTemp["Calculate"]) + 1;
                        if (Convert.ToInt32(drTemp["Calculate"]) >= 2 && Convert.ToInt32(drTemp["Calculate"]) < 99)
                            {
                                drRouting["Routing_Group"] = Convert.ToInt32(drTemp["Routing_Group"]);
                                drRouting["Calculate"] = Convert.ToInt32(drTemp["Calculate"]) + 1;

                            }
                            else if (Convert.ToInt32(drTemp["Calculate"]) == 99)
                            {
                                drRouting["Routing_Group"] = Convert.ToInt32(drTemp["Routing_Group"]) + 1;
                                drRouting["Calculate"] = 2;
                            }
                        
                    }
                    else
                    {
                        DataRow drTemp3 = dtSerial.Select("1=1 ", "Routing_Group,Calculate ASC").LastOrDefault();
                        Serial = Convert.ToInt32(drTemp3["Calculate"]) + 1;

                        if (Convert.ToInt32(drTemp3["Calculate"]) >= 1 && Convert.ToInt32(drTemp3["Calculate"]) < 99)
                            {
                                drRouting["Routing_Group"] = Convert.ToInt32(drTemp3["Routing_Group"]);
                                drRouting["Calculate"] = Convert.ToInt32(drTemp3["Calculate"]) + 1;

                            }
                            else if (Convert.ToInt32(drTemp3["Calculate"]) == 99)
                            {
                                drRouting["Routing_Group"] = Convert.ToInt32(drTemp3["Routing_Group"]) + 1;
                                drRouting["Calculate"] = 1;
                            }
                        
                    }
                    GROUP_CODE = Convert.ToInt32(drRouting["Routing_Group"]);
                    iCount = Convert.ToInt32(drRouting["Calculate"]);
                    Routing_Group = Convert.ToInt32(drRouting["Routing_Group"]);
                    Calculate = Convert.ToInt32(drRouting["Calculate"]);
                    drRouting["Serial"] = Serial;

                    drow["Routing_Group"] = Routing_Group;
                    drow["Calculate"] = Calculate;
                    drow["Calculate_Desc"] = CountDecription; //如何取值
                    dtSerial.Rows.Add(drow);
                }
            }

           

            drRouting["Serial"] = Serial;
            drRouting["Routing_Group"] = Routing_Group;
            drRouting["Calculate"] = Calculate;
            drRouting["Calculate_Desc"] = CountDecription; //如何取值
            drRouting["Date_TS"] = DateTime.Now;
            dtRouting.Rows.Add(drRouting);


            //if (drTemp2.Length > 0)
            //{
            //    GROUP_CODE = Convert.ToInt32(drTemp2[0]["Routing_Group"]);
            //    iCount = Convert.ToInt32(drTemp2[0]["Calculate"]);
            //}
            //else
            //{
            //    if (drTemp1.Length > 0)
            //    {
            //        GROUP_CODE = Convert.ToInt32(drTemp1[0]["Routing_Group"]);
            //        iCount = Convert.ToInt32(drTemp1[0]["Calculate"]);

            //    }
            //    else
            //    {
            //        if (first)
            //        {
            //            Serial = 1;
            //            if (drser != null)
            //            {
            //                if (Flag == 1)//Laminate
            //                {
            //                    if (Convert.ToInt32(drser["Calculate"]) >= 2 && Convert.ToInt32(drser["Calculate"]) < 99)
            //                    {
            //                        drRouting["Routing_Group"] = Convert.ToInt32(drser["Routing_Group"]);
            //                        drRouting["Calculate"] = Convert.ToInt32(drser["Calculate"]) + 1;

            //                    }
            //                    else if (Convert.ToInt32(drser["Calculate"]) == 99)
            //                    {
            //                        drRouting["Routing_Group"] = Convert.ToInt32(drser["Routing_Group"]) + 1;
            //                        drRouting["Calculate"] = 2;
            //                    }
            //                }
            //                else
            //                {
            //                    if (Convert.ToInt32(drser["Calculate"]) >= 1 && Convert.ToInt32(drser["Calculate"]) < 99)
            //                    {
            //                        drRouting["Routing_Group"] = Convert.ToInt32(drser["Routing_Group"]);
            //                        drRouting["Calculate"] = Convert.ToInt32(drser["Calculate"]) + 1;

            //                    }
            //                    else if (Convert.ToInt32(drser["Calculate"]) == 99)
            //                    {
            //                        drRouting["Routing_Group"] = Convert.ToInt32(drser["Routing_Group"]) + 1;
            //                        drRouting["Calculate"] = 1;
            //                    }
            //                }
            //            }
            //            else
            //            {
            //                if (Flag == 1)//Laminate
            //                {
            //                    drRouting["Routing_Group"] = 44000001;
            //                    drRouting["Calculate"] = 2;
            //                }
            //                else if (Flag == 2)//Panel
            //                {
            //                    drRouting["Routing_Group"] = 43000001;
            //                    drRouting["Calculate"] = 1;
            //                }
            //                else if (Flag == 3)//Seating
            //                {
            //                    drRouting["Routing_Group"] = 41000001;
            //                    drRouting["Calculate"] = 1;
            //                }
            //                else if (Flag == 4)//MCG
            //                {
            //                    drRouting["Routing_Group"] = 47000001;
            //                    drRouting["Calculate"] = 1;
            //                }
            //            }
            //            GROUP_CODE = Convert.ToInt32(drRouting["Routing_Group"]);
            //            iCount = Convert.ToInt32(drRouting["Calculate"]);
            //            Routing_Group = Convert.ToInt32(drRouting["Routing_Group"]);
            //            Calculate = Convert.ToInt32(drRouting["Calculate"]);
            //        }
            //        else
            //        {

            //            if (drTemp != null)
            //            {
            //                Serial = Convert.ToInt32(drTemp["Serial"]) + 1;
            //                if (Flag == 1)
            //                {
            //                    if (Convert.ToInt32(drTemp["Calculate"]) >= 2 && Convert.ToInt32(drTemp["Calculate"]) < 99)
            //                    {
            //                        drRouting["Routing_Group"] = Convert.ToInt32(drTemp["Routing_Group"]);
            //                        drRouting["Calculate"] = Convert.ToInt32(drTemp["Calculate"]) + 1;

            //                    }
            //                    else if (Convert.ToInt32(drTemp["Calculate"]) == 99)
            //                    {
            //                        drRouting["Routing_Group"] = Convert.ToInt32(drTemp["Routing_Group"]) + 1;
            //                        drRouting["Calculate"] = 2;
            //                    }
            //                }
            //                else
            //                {
            //                    if (Convert.ToInt32(drTemp["Calculate"]) >= 1 && Convert.ToInt32(drTemp["Calculate"]) < 99)
            //                    {
            //                        drRouting["Routing_Group"] = Convert.ToInt32(drTemp["Routing_Group"]);
            //                        drRouting["Calculate"] = Convert.ToInt32(drTemp["Calculate"]) + 1;

            //                    }
            //                    else if (Convert.ToInt32(drTemp["Calculate"]) == 99)
            //                    {
            //                        drRouting["Routing_Group"] = Convert.ToInt32(drTemp["Routing_Group"]) + 1;
            //                        drRouting["Calculate"] = 1;
            //                    }
            //                }
            //            }
            //            else
            //            {
            //                DataRow[] drTemp3 = dtSerial.Select("Calculate_Desc='" + CountDecription + "'");
            //                if (Flag == 1)//Laminate
            //                {
            //                    drRouting["Routing_Group"] = 44000001;
            //                    drRouting["Calculate"] = 2;
            //                }
            //                else if (Flag == 2)//Panel
            //                {
            //                    drRouting["Routing_Group"] = 43000001;
            //                    drRouting["Calculate"] = 1;
            //                }
            //                else if (Flag == 3)//Seating
            //                {
            //                    drRouting["Routing_Group"] = 41000001;
            //                    drRouting["Calculate"] = 1;
            //                }
            //                else if (Flag == 4)//MCG
            //                {
            //                    drRouting["Routing_Group"] = 47000001;
            //                    drRouting["Calculate"] = 1;
            //                }
            //            }
            //            GROUP_CODE = Convert.ToInt32(drRouting["Routing_Group"]);
            //            iCount = Convert.ToInt32(drRouting["Calculate"]);
            //            Routing_Group = Convert.ToInt32(drRouting["Routing_Group"]);
            //            Calculate = Convert.ToInt32(drRouting["Calculate"]);
            //        }
            //        //}
            //        //存在的数据不需要插入到数据库                    
            //        drRouting["Serial"] = Serial;
            //        drRouting["Routing_Group"] = Routing_Group;
            //        drRouting["Calculate"] = Calculate;
            //        drRouting["Calculate_Desc"] = CountDecription; //如何取值
            //        drRouting["Date_TS"] = DateTime.Now;
            //        dtRouting.Rows.Add(drRouting);
            //       // first = false;
            //    }
            //}
            return dtRouting;
        }
    }
}
